GooglePayLauncher

A drop-in class that presents a Google Pay sheet to collect a customer's payment details. When the launcher finishes, a Result will be returned via ResultCallback.onResult.

Use of this class will typically look like this:

  1. Create an instance of this class

  2. Create a button for Google Pay (see GooglePayButton) and hook it up to a click listener

    • Optionally use the readyCallback property to set visibility or enabled states

  3. In the click handler, do the following:

    • Ensure Google Pay is ready (see the readyCallback property (or use the isReady property)

    • Call present to present the Google Pay sheet and begin the payment process

  4. Use the data passed to ResultCallback.onResult to retrieve payment details and process the payment

Important:

The resultCallback property must be set before calling present



Example Implementation

class PaymentActivity : ComponentActivity(), ReadyCallback, ResultCallback {
lateinit var button: GooglePayButton
lateinit var launcher: IGooglePayLauncher

val gatewayJson = JSONObject()
.put("gateway", "freedompay")
.put("gatewayMerchantId", "MyMerchantId")
.toString()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_payment_view)

button = findViewById<GooglePayButton>(R.id.my_google_pay_button)
button.isEnabled = false
button.setOnClickListener {
if (!launcher.isReady) {
return@setOnClickListener
}

launcher.present(100)
}

launcher = GooglePayLauncher(
activity = this,
readyCallback = this,
resultCallback = this,
config = Configuration(
environment = SdkEnvironment.Test,
gatewayParametersJson = gatewayJson,
companyName = "Test Company"
)
)
}

// Update the button state when Google Pay readiness changes
override fun onReady(isReady: Boolean) {
button.isEnabled = isReady
}

// Handle the result of the Google Pay sheet
override fun onResult(result: Result) {
when (result) {
is Result.Completed -> {
// Submit the payment data for payment processing (generally to Olo's ordering API)
// This can be retrieved from result.paymentData
}
is Result.Canceled -> {
// The user canceled the payment
}
is Result.Failed -> {
// An error occurred while attempting to get payment details
// Details about the error can be retrieved from result.error
}
}
}
}

Constructors

Link copied to clipboard
constructor(activity: ComponentActivity, config: Configuration, readyCallback: ReadyCallback? = null, resultCallback: ResultCallback? = null)

Constructor for using this class from a ComponentActivity.

constructor(fragment: Fragment, config: Configuration, readyCallback: ReadyCallback? = null, resultCallback: ResultCallback? = null)

Constructor for using this class from a Fragment.

Properties

Link copied to clipboard
open override var config: Configuration

The configuration for Google Pay

Link copied to clipboard
open override val isReady: Boolean

True if Google Pay is available and ready to present.

Link copied to clipboard
open override var readyCallback: ReadyCallback?

Optional callback to be notified when Google Pay is ready.

Link copied to clipboard
open override var resultCallback: ResultCallback?

Callback to get the results of the Google Pay flow.

Functions

Link copied to clipboard
open override fun present(amount: Long)
open override fun present(amount: Long, checkoutStatus: CheckoutStatus?)
open override fun present(amount: Long, checkoutStatus: CheckoutStatus?, totalPriceLabel: String?)
open override fun present(amount: Long, checkoutStatus: CheckoutStatus?, totalPriceLabel: String?, lineItems: List<LineItem>?)
open override fun present(amount: Long, checkoutStatus: CheckoutStatus?, totalPriceLabel: String?, lineItems: List<LineItem>?, validateLineItems: Boolean)
open override fun present(amount: Long, checkoutStatus: CheckoutStatus?, totalPriceLabel: String?, lineItems: List<LineItem>?, validateLineItems: Boolean, transactionId: String?)

Presents the Google Pay sheet. Use resultCallback to listen for and handle the result of showing the Google Pay sheet to the user.